package com.leiyuee.mybatis.util;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class CalendarUtil {


	private static Calendar calendar = null;

	private static Calendar getCalendar(){
		if (calendar==null){
			calendar = Calendar.getInstance();
		}
		return calendar;
	}


	public static Calendar getDate2Calendar(Date date){
        Calendar calendar = getCalendar();
        calendar.setTime(date);
        return calendar;
    }

    public static Date getNextMonday(Date date){
//		Calendar calendar = setTime2Zero(getDate2Calendar(date));
//		calendar.set(Calendar.WEEK_OF_MONTH,calendar.get(Calendar.WEEK_OF_MONTH)+1);
//		calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
//		return calendar.getTime();
	    return getNextMonday(date,1);
    }

    public static Date getNextFirday(Date date){
//		Calendar calendar = setTime2Zero(getDate2Calendar(date));
//		calendar.set(Calendar.WEEK_OF_MONTH,calendar.get(Calendar.WEEK_OF_MONTH)+1);
//		calendar.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
//		return calendar.getTime();
	    return getNextFirday(date,1);
    }

    public static Date getNextMonday(Date date,Integer count){
		Calendar calendar = setTime2Zero(getDate2Calendar(date));
		calendar.set(Calendar.WEEK_OF_MONTH,calendar.get(Calendar.WEEK_OF_MONTH)+count);
		calendar.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);
		System.out.println(calendar.getTime());
		return calendar.getTime();
    }

    public static Date getNextFirday(Date date,Integer count){
		Calendar calendar = setTime2End(getDate2Calendar(date));
		calendar.set(Calendar.WEEK_OF_MONTH,calendar.get(Calendar.WEEK_OF_MONTH)+count);
		calendar.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
	    System.out.println(calendar.getTime());
		return calendar.getTime();
    }



    public static List<Date> getNextPlanWeek(Date date){
        return getNextPlanWeek(date,1);
    }

    public static List<Date> getNextPlanWeek(Date date,Integer count){
        List<Date> nextWeek = new ArrayList<>(2);
        nextWeek.add(getNextMonday(date,count));
        nextWeek.add(getNextFirday(date,count+1));
        return nextWeek;
    }

    public static List<Date> getNextWeek(Date date){
        return getNextWeek(date,1);
    }

    public static List<Date> getNextWeek(Date date,Integer count){
        List<Date> nextWeek = new ArrayList<>(2);
        nextWeek.add(getNextMonday(date,count));
        nextWeek.add(getNextFirday(date,count));
        return nextWeek;
    }

    private static Calendar setTime2Zero(Calendar calendar){
		calendar.set(Calendar.AM_PM,Calendar.AM);
		calendar.set(Calendar.HOUR,0);
		calendar.set(Calendar.MINUTE,0);
		calendar.set(Calendar.SECOND,0);
		return calendar;
    }

    private static Calendar setTime2End(Calendar calendar){
		calendar.set(Calendar.AM_PM,Calendar.PM);
		calendar.set(Calendar.HOUR,23);
		calendar.set(Calendar.MINUTE,59);
		calendar.set(Calendar.SECOND,59);
		return calendar;
    }


	public static void main(String[] args) {
//		System.out.println(CalendarUtil.getNextMonday(new Date()));
		System.out.println(CalendarUtil.getNextFirday(new Date(),2));
		System.out.println(JSON.toJSONString(CalendarUtil.getNextPlanWeek(new Date())));
	}
}
